//
//  SSProgressView.swift
//  weibo
//
//  Created by 邵帅 on 16/7/7.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit

class SSProgressView: UIImageView {
    
    var progress: CGFloat = 0 {
        
        didSet{
            
            loadView.progress = progress
            
        }
        
        
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        addSubview(loadView)
        
        loadView.backgroundColor = UIColor.clearColor()
        
        loadView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self.snp_center)
            make.width.equalTo(self.snp_width)
            make.height.equalTo(snp_width)
        }
    }

    
    // 懒加载
    private lazy var loadView: LoadView = {
        
        let view = LoadView()
        
        return view
    }()
}

class LoadView: UIView {
    
    var progress: CGFloat = 0 {
        
        didSet{
            
            setNeedsDisplay()
        }
    }
    
    override func drawRect(rect: CGRect) {
        
        let center = CGPointMake(rect.width * 0.5, rect.height * 0.5)
        
        let radius = min(rect.width, rect.height) * 0.5
        
        let startAngle = CGFloat(0)
        
        let endAngle = 2 * CGFloat(M_PI) * progress
        
        let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        
        path.addLineToPoint(center)
        
        UIColor.redColor().setFill()
        path.closePath()
        
        path.fill()
        
    }
}